package cn.lili.modules.goods.entity.dos;

import cn.hutool.core.text.CharSequenceUtil;
import cn.lili.common.enums.ResultCode;
import cn.lili.common.exception.ServiceException;
import cn.lili.mybatis.model.BaseStandardEntity;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableLogic;
import com.baomidou.mybatisplus.annotation.TableName;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.validation.constraints.*;
import java.math.BigDecimal;

/**
 * 商品分类
 *
 * @author pikachu
 * @since 2020-02-18 15:18:56
 */
@Data
@TableName("li_category")
@ApiModel(value = "商品分类")
@AllArgsConstructor
@NoArgsConstructor
public class Category extends BaseStandardEntity {

    private static final long serialVersionUID = 1L;

    @NotEmpty(message = "分类名称不能为空")
    @Size(max = 20)
    @ApiModelProperty(value = "分类名称")
    private String name;

    @NotEmpty(message = "请选择父分类")
    @ApiModelProperty(value = "父id, 根节点为0")
    private String parentId;

    @NotNull(message = "层级不能为空")
    @Min(value = 0, message = "层级需要大于0")
    @Max(value = 2, message = "层级最大为2")
    @ApiModelProperty(value = "层级, 从0开始")
    private Integer level;

    @NotNull(message = "排序值不能为空")
    @Max(value = 999, message = "排序值最大999")
    @ApiModelProperty(value = "排序值")
    private BigDecimal sortOrder;

    @ApiModelProperty(value = "佣金比例")
    private Double commissionRate;

    @ApiModelProperty(value = "分类图标")
    private String image;

    @ApiModelProperty(value = "是否支持频道")
    private Boolean supportChannel;

    @TableField(fill = FieldFill.INSERT)
    @ApiModelProperty(value = "禁用标识 true/false 禁用/启用", hidden = true)
    private Boolean useFlag;

    public void checkParams() {
        if (CharSequenceUtil.isEmpty(this.name)) {
            throw new ServiceException(ResultCode.PARAMS_ERROR, "分类名称不能为空");
        }
        if (this.name.length() > 20) {
            throw new ServiceException(ResultCode.PARAMS_ERROR, "分类名称不能超过20个字符");
        }
        if (CharSequenceUtil.isEmpty(this.parentId)) {
            throw new ServiceException(ResultCode.PARAMS_ERROR, "请选择父分类，如果是顶级父类，请传递0");
        }
        if (level == null) {
            throw new ServiceException(ResultCode.PARAMS_ERROR, "分类层级不能为空");
        }
        if (level < 0 || level > 2) {
            throw new ServiceException(ResultCode.PARAMS_ERROR, "分类层级参数无效");
        }
        if (sortOrder == null) {
            this.sortOrder = new BigDecimal(1);
        }
        if (this.sortOrder.compareTo(BigDecimal.ZERO) < 0 || this.sortOrder.compareTo(new BigDecimal(999)) > 0) {
            throw new ServiceException(ResultCode.PARAMS_ERROR, "排序值最大999");
        }
        if (this.level == 2) {
            if (this.commissionRate == null) {
                throw new ServiceException(ResultCode.PARAMS_ERROR, "请输入佣金比例");
            }
            if (this.commissionRate > 100 || commissionRate < 0) {
                throw new ServiceException(ResultCode.PARAMS_ERROR, "佣金比例范围为0-100");
            }
        }

    }
}